package view.mt4j.scene.menu;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import model.ApplicationModel;
import model.ApplicationModel.Property;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.input.inputProcessors.globalProcessors.CursorTracer;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import controller.ApplicationController;
import controller.view.MenuController;

import view.interfaces.screen.menu.AppHomeView;
import view.mt4j.util.ButtonListener;
import view.mt4j.util.ProcessorHelper;
import static app.ViewConstants.*;

public class AppHomeScene extends AbstractScene implements AppHomeView,
		PropertyChangeListener {
	private static final Logger log = LoggerFactory
			.getLogger(AppHomeScene.class);
	private ApplicationModel model;

	private ApplicationController applicationController;
	private MenuController menuController;

	private MTRectangle topContainer;
	private float ratio = 1f;

	private MTRoundRectangle newProjectButtonShape;
	private MTTextArea newProjectButtonText;

	private MTRoundRectangle manageProjectsButtonShape;
	private MTTextArea manageProjectsButtonText;

	private MTRoundRectangle joinProjectButtonShape;
	private MTTextArea joinProjectButtonText;

	private MTRoundRectangle goProjectButtonShape;
	private MTTextArea goProjectButtonText;

	public AppHomeScene(MTApplication mtApplication, ApplicationModel model) {
		super(mtApplication, "MtStatetchartHome");
		this.model = model;

		// Set the background color
		setClearColor(MENU_BACKGROUND_COLOR);

		topContainer = new MTRectangle(getMTApplication(),
				START_MENU_SCENE_MAX_WIDTH, START_MENU_SCENE_MAX_HEIGHT);
		topContainer.setStrokeColor(COLOR_ORANGE);
		topContainer.setFillColor(MENU_BACKGROUND_COLOR);

		addNewProjectButton(topContainer, 400, 80);
		addManageProjectsButton(topContainer, 400, 80);
		addJoinProjectButton(topContainer, 400, 80);
		addGoProjectButton(topContainer, 400, 80);

		getCanvas().addChild(topContainer);

		scaleMenuScene();
		this.registerGlobalInputProcessor(new CursorTracer(mtApplication, this));

		model.addPropertyChangeListener(Property.CURRENT_PROJECT, this);

	}

	private void scaleMenuScene() {
		ratio = (getMTApplication().screenWidth <= START_MENU_SCENE_MAX_WIDTH) ? 1f
				: START_MENU_SCENE_MAX_WIDTH
						/ (float) getMTApplication().screenWidth;
		topContainer.scale(ratio, ratio, 1f,
				topContainer.getCenterPointGlobal());
	}

	@Override
	public void setController(ApplicationController applicationController) {
		log.debug("setting controller ...");
		this.applicationController = applicationController;
		this.menuController = applicationController.getMenuController();
		log.debug("setting controller DONE");
	}

	private void addNewProjectButton(MTRectangle container, float width,
			float height) {
		newProjectButtonShape = createButtonShape(container, width, height);
		container.addChild(newProjectButtonShape);
		newProjectButtonShape.setPositionRelativeToParent(new Vector3D(
				container.getWidthXY(TransformSpace.LOCAL) / 4f, container
						.getHeightXY(TransformSpace.LOCAL) / 4f));
		newProjectButtonText = createButtonText("Nouveau projet",
				newProjectButtonShape);
		newProjectButtonShape.addChild(newProjectButtonText);

		registerProcessor(container, newProjectButtonText,
				newProjectButtonShape);

		newProjectButtonText.registerInputProcessor(new TapProcessor(
				getMTApplication()));
		newProjectButtonShape.registerInputProcessor(new TapProcessor(
				getMTApplication()));

		ProcessorHelper.delegateGestureEventTo(newProjectButtonShape,
				TapProcessor.class, newProjectButtonText);
		newProjectButtonShape.addGestureListener(TapProcessor.class,
				new ButtonListener(newProjectButtonShape, newProjectButtonText,
						COLOR_ORANGE, MTColor.BLACK) {
					@Override
					public void onClick() {
						System.out.println("NOUVEAU PROJET BTN CLICK !");
						applicationController.showNewProjectMenuView();
					}
				});
	}

	private void addManageProjectsButton(MTRectangle container, float width,
			float height) {
		manageProjectsButtonShape = createButtonShape(container, width, height);
		container.addChild(manageProjectsButtonShape);
		manageProjectsButtonShape.setPositionRelativeToParent(new Vector3D(
				container.getWidthXY(TransformSpace.LOCAL) * 3f / 4f, container
						.getHeightXY(TransformSpace.LOCAL) / 4f));
		manageProjectsButtonText = createButtonText("Ouvrir projet",
				manageProjectsButtonShape);
		manageProjectsButtonShape.addChild(manageProjectsButtonText);

		registerProcessor(container, manageProjectsButtonText,
				manageProjectsButtonShape);

		manageProjectsButtonText.registerInputProcessor(new TapProcessor(
				getMTApplication()));
		manageProjectsButtonShape.registerInputProcessor(new TapProcessor(
				getMTApplication()));

		ProcessorHelper.delegateGestureEventTo(manageProjectsButtonShape,
				TapProcessor.class, manageProjectsButtonText);
		manageProjectsButtonShape.addGestureListener(TapProcessor.class,
				new ButtonListener(manageProjectsButtonShape,
						manageProjectsButtonText, COLOR_ORANGE, MTColor.BLACK) {
					@Override
					public void onClick() {
						System.out.println("GERER PROJET BTN CLICK !");
						applicationController.showManageProjectsMenuView();
					}
				});
	}

	private void addJoinProjectButton(MTRectangle container, float width,
			float height) {
		joinProjectButtonShape = createButtonShape(container, width, height);
		container.addChild(joinProjectButtonShape);
		joinProjectButtonShape.setPositionRelativeToParent(new Vector3D(
				container.getWidthXY(TransformSpace.LOCAL) / 4f, container
						.getHeightXY(TransformSpace.LOCAL) * 3f / 4f));
		joinProjectButtonText = createButtonText("Rejoindre projet",
				joinProjectButtonShape);
		joinProjectButtonShape.addChild(joinProjectButtonText);

		registerProcessor(container, joinProjectButtonText,
				joinProjectButtonShape);

		joinProjectButtonText.registerInputProcessor(new TapProcessor(
				getMTApplication()));
		joinProjectButtonShape.registerInputProcessor(new TapProcessor(
				getMTApplication()));

		ProcessorHelper.delegateGestureEventTo(joinProjectButtonShape,
				TapProcessor.class, joinProjectButtonText);
		joinProjectButtonShape.addGestureListener(TapProcessor.class,
				new ButtonListener(joinProjectButtonShape,
						joinProjectButtonText, COLOR_ORANGE, MTColor.BLACK) {
					@Override
					public void onClick() {
						System.out.println("REJOINDRE PROJET BTN CLICK !");
						applicationController.showNewProjectMenuView();
					}
				});
	}

	private void addGoProjectButton(MTRectangle container, float width,
			float height) {
		goProjectButtonShape = createButtonShape(container, width, height);
		container.addChild(goProjectButtonShape);
		goProjectButtonShape.setPositionRelativeToParent(new Vector3D(container
				.getWidthXY(TransformSpace.LOCAL) * 3f / 4f, container
				.getHeightXY(TransformSpace.LOCAL) * 3f / 4f));
		goProjectButtonText = createButtonText("Go projet",
				goProjectButtonShape);
		goProjectButtonShape.addChild(goProjectButtonText);

		registerProcessor(container, goProjectButtonText, goProjectButtonShape);

		goProjectButtonText.registerInputProcessor(new TapProcessor(
				getMTApplication()));
		goProjectButtonShape.registerInputProcessor(new TapProcessor(
				getMTApplication()));

		ProcessorHelper.delegateGestureEventTo(goProjectButtonShape,
				TapProcessor.class, goProjectButtonText);
		setGoProjectButtonEnabled(false);
	}

	private void registerProcessor(MTRectangle container, MTTextArea textArea,
			MTRoundRectangle buttonShape) {
		textArea.removeAllGestureEventListeners();
		ProcessorHelper.delegateDragScaleRotateEventTo(buttonShape, textArea);
		buttonShape.removeAllGestureEventListeners();
		ProcessorHelper.delegateDragScaleRotateEventTo(container, buttonShape);
	}

	private MTTextArea createButtonText(String text, MTRoundRectangle container) {
		MTTextArea buttonText = new MTTextArea(getMTApplication(), FontManager
				.getInstance().createFont(getMTApplication(), "arial.ttf", 50,
						MTColor.WHITE));
		buttonText.setText(text);
		buttonText.setNoFill(true);
		buttonText.setNoStroke(true);
		buttonText.setPositionRelativeToParent(new Vector3D(container
				.getWidthXY(TransformSpace.LOCAL) / 2f, container
				.getHeightXY(TransformSpace.LOCAL) / 2f));
		return buttonText;
	}

	private MTRoundRectangle createButtonShape(MTRectangle container,
			float width, float height) {
		MTRoundRectangle buttonShape = new MTRoundRectangle(getMTApplication(),
				0, 0, 0, width, height, 10, 10);

		buttonShape.setFillColor(COLOR_ORANGE);
		return buttonShape;
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(
				ApplicationModel.Property.CURRENT_PROJECT.name())) {
			setGoProjectButtonEnabled(evt.getNewValue() != null);
		}
	}

	private void setGoProjectButtonEnabled(boolean enabled) {
		if (enabled) {
			goProjectButtonText.setFontColor(MTColor.WHITE);
			goProjectButtonShape.setFillColor(COLOR_ORANGE);
			goProjectButtonShape.addGestureListener(TapProcessor.class,
					new ButtonListener(goProjectButtonShape,
							goProjectButtonText, COLOR_ORANGE, MTColor.BLACK) {
						@Override
						public void onClick() {
							applicationController.showProjectView();
						}
					});
		} else {
			goProjectButtonText.setFontColor(MTColor.BLACK);
			goProjectButtonShape.setFillColor(MTColor.GRAY);
			goProjectButtonShape.removeAllGestureEventListeners();
		}
	}
}
